package com.my.screenadapter.View;

import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 张英杰 on 2017/9/29.
 */

public class TextViewUtils {

    /**
     * 设置edittext是否可以输入
     *
     * @param editable
     * @param views
     */
    public static void setEditTextable(boolean editable, View... views) {
        if (views == null) {
            return;
        }
        for (int i = 0; i < views.length; i++) {
            View view = views[i];
            if (view != null && (view instanceof TextView)) {
                TextView editText = (TextView) view;
                if (!editable) { // disable editing password
                    editText.setFocusable(false);
                    editText.setFocusableInTouchMode(false); // user touches widget on phone with touch screen
                    editText.setClickable(false); // user navigates with wheel and selects widget
                } else { // enable editing of password
                    editText.setFocusable(true);
                    editText.setFocusableInTouchMode(true);
                    editText.setClickable(true);
                }
            }
        }
    }

    /**
     * 设置edittext是否可以输入
     *
     * @param editable
     * @param views
     */
    public static void setEditTextable(boolean editable, ViewGroup group, View... views) {
        if (group == null) {
            return;
        }
        List<TextView> mlistTextview = new ArrayList<>();
        getTextViews(group,mlistTextview);
        List<View> tempNoChoiceTextView = new ArrayList<>();
        for (int i = 0; i < mlistTextview.size(); i++) {
            View childAt = mlistTextview.get(i);
            int id = childAt.getId();
            boolean isHave = false;//是否在选择中
            a:
            for (int j = 0; views != null && j < views.length; j++) {
                if ((views[j] != null) && (id == views[j].getId())) {
                    isHave = true;
                    break a;
                }
            }
            //如果没有存在 特定的view
            if (!isHave && (childAt instanceof TextView)) {
                tempNoChoiceTextView.add(childAt);
            }
        }
        if (tempNoChoiceTextView.size() > 0) {
            View[] noChoices = new View[tempNoChoiceTextView.size()];
            for (int i = 0; i < tempNoChoiceTextView.size(); i++) {
                noChoices[i] = tempNoChoiceTextView.get(i);
            }
            setEditTextable(!editable, noChoices);
        }
        setEditTextable(editable, views);
    }

    //查找所有的textview
    private static void getTextViews(ViewGroup viewGroup, List<TextView> list) {
        if (viewGroup == null) {
            return;
        }
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            View childAt = viewGroup.getChildAt(i);
            if (childAt != null && (childAt instanceof TextView)) {
                //包含 textview
                list.add((TextView) childAt);
            } else if (childAt != null && (childAt instanceof ViewGroup)) {
                getTextViews((ViewGroup) childAt, list);
            }
        }
    }

}
